﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;

namespace sydfdl.TMS.BLL
{

    public class BLLT_B_Menu
    {
        /// <summary>
        /// 获取菜单权限
        /// </summary>
        /// <param name="userCode"></param>
        /// <param name="functionCode"></param>
        /// <returns></returns>
        public DataTable GetUserMenuList(string userCode, string functionCode)
        {
            DataTable dt = new DataTable();

            try
            {
                SqlParameter[] para = { 
                                          new SqlParameter("@UserCode", SqlDbType.NVarChar, 50),
                                          new SqlParameter("@FunctionCode", SqlDbType.NVarChar, 50)
                                       };

                para[0].Value = userCode;
                para[1].Value = functionCode;

                dt = SqlHelper.ExecuteProcedureToDataSet("P_B_Home_GetUserMenuList", para, "dt").Tables[0];

            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }

            return dt;
        }

        public List<T_B_Menu> GetList()
        {
            List<T_B_Menu> list = new List<T_B_Menu>();
            #region 获取数据
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_GetMenu", null))
            {
                while (dr.Read() && dr.HasRows)
                {
                    T_B_Menu dto = new T_B_Menu();
                    dto.MID = dr.GetGuid(0);
                    dto.FID = dr.GetValue(1) is DBNull ? Guid.Empty : dr.GetGuid(1);
                    dto.MName = dr.GetString(2);
                    dto.MUrl = dr.GetString(3);
                    dto.IsValid = dr.GetBoolean(4);
                    dto.IsHidden = dr.GetBoolean(5);
                    dto.MSort = dr.GetDouble(6);
                    dto.IsHasChild = dr.GetBoolean(7);
                    dto.IsRoot = dr.GetBoolean(8);
                    dto.IsLeaf = dr.GetBoolean(9);
                    dto.MLevel = dr.GetInt16(10);
                    //dto.ButtonXML = dr.GetValue(11) is DBNull ? string.Empty : dr.GetSqlXml(11).Value;
                    list.Add(dto);
                }
            }
            #endregion
            return list;
        }

        public List<T_B_MenuXML> GetMenuXML(Guid MID)
        {
            List<T_B_MenuXML> list = new List<T_B_MenuXML>();
            SqlParameter[] para = { 
                                  new SqlParameter("@MID",SqlDbType.UniqueIdentifier,16),
                                  };
            para[0].Value = MID;
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_GetMenuXML", para))
            {
                while (dr.Read() && dr.HasRows)
                {
                    T_B_MenuXML dto = new T_B_MenuXML();
                    dto.ID = dr.GetString(0);
                    dto.Text = dr.GetString(1);
                    list.Add(dto);
                }
            }
            return list;

        }

        /// <summary>
        /// 操作数据
        /// </summary>
        /// <param name="xml">第一个为添加 ；第二个为修改；第三个为删除；</param>
        /// <returns></returns>
        public string Save(params IEnumerable<T_B_Menu>[] xml)
        {
            string addXmL = EFHelp<T_B_Menu>.GetXML(xml[0]);
            string editXML = EFHelp<T_B_Menu>.GetXML(xml[1]);
            string delXML = EFHelp<T_B_Menu>.GetXML(xml[2]);
            SqlParameter[] para = { 
                                  new SqlParameter("@addXmL",SqlDbType.Xml,-1),
                                  new SqlParameter("@editXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@delXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@userXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                  };
            para[0].Value = addXmL;
            para[1].Value = editXML;
            para[2].Value = delXML;
            para[3].Value = null;
            para[4].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_SaveMenu", para);
            return (para[4].Value ?? string.Empty).ToString();
        }


        /// <summary>
        /// 操作数据
        /// </summary>
        /// <param name="xml">第一个为添加 ；第二个为修改；第三个为删除；</param>
        /// <param name="mid">MID</param>
        /// <returns></returns>
        public string SaveXML(IEnumerable<T_B_MenuXML> xml, Guid mid)
        {
            string addXmL = EFHelp<T_B_MenuXML>.GetXML(xml);
            SqlParameter[] para = { 
                                  new SqlParameter("@addXmL",SqlDbType.Xml,-1),
                                  new SqlParameter("@mid",SqlDbType.UniqueIdentifier,16),
                                  new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                  };
            para[0].Value = addXmL;
            para[1].Value = mid;
            para[2].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_SaveMenuXML", para);
            return (para[2].Value ?? string.Empty).ToString();
        }
    }
}
